package linter.controller.blog;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import linter.POJO.DO.Comments;
import linter.POJO.DO.Posts;
import linter.POJO.DTO.PostsDTO;
import linter.POJO.DTO.UserDTO;
import linter.POJO.UtilDTO.BlogCreateUtilDTO;
import linter.POJO.UtilDTO.PFUtilDTO;
import linter.custom.ResultVO;
import linter.service.CommentsService;
import linter.service.LikesContentService;
import linter.service.PostsService;
import linter.service.UserService;
import linter.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static linter.custom.CustomResultType.*;

/**
 *
 * @author
 * @date 2020/11/17 16:37
 */
@Api(tags = "论坛主体控制器")
@RestController
@Slf4j
@RequestMapping("/Blog")
public class BlogController {

    @Resource
    PostsService postsService;
    @Resource
    UserService userService;
    @Resource
    LikesContentService likesContentService;
    @Resource
    CommentsService commentsService;

    @GetMapping("/show/getUserByUsername/{username}")
    @ApiOperation("根据用户名查找用户")
    public ResultVO<UserDTO> getUserByUsername(@PathVariable String username) {
        UserDTO userDTO = userService.getUserByUsername(username);
        return ResultVO.success(userDTO);
    }

    /**
     *根据不同的类型展示文章
     *
     * @param pageNum 第几页
     * @param pageSize 每页展示几个
     * @param sortType 展示的类型是什么？ 1：推荐 2：根据收藏 3：根据点赞 4：根据浏览 5：根据评论
     * @return
     */
    @PostMapping("/BlogSort")
    @ApiOperation("文章排序")
    public ResultVO<PageInfo<PostsDTO>> blogSort(@RequestBody PostsDTO postsDTO){
        log.info("调用文章排序接口");
        //TODO：需要进行推荐算法的应用
        return ResultVO.success(postsService.selectBySortType(postsDTO.getPageNum(),postsDTO.getPageSize(),postsDTO.getSortType()));
    }

    @PostMapping("/BlogCreate")
    @ApiOperation("文章创建")
    public ResultVO<String> blogCreate(@RequestBody BlogCreateUtilDTO blogCreateUtilDTO){
        log.info("调用文章创建接口");
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        String title = blogCreateUtilDTO.getTitle();
        Object content = blogCreateUtilDTO.getContent();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String jsonContent = objectMapper.writeValueAsString(content);
            if (uuid != null){
                return ResultVO.success(postsService.createPosts(title,jsonContent,uuid));
            }
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/BlogSelect")
    @ApiOperation("文章查询")
    public ResultVO<?> blogSelect(@RequestBody PostsDTO postsDTO){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){
            return ResultVO.success(postsService.selectPosts(postsDTO,uuid));
        }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/getBlogByPostsId")
    @ApiOperation("通过文章id获取文章")
    public ResultVO<Posts> getBlogByPostsId(@RequestParam Integer postsId){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(postsService.getBlogByPostsId(postsId)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/getBlogNotReviewed")
    @ApiOperation("获取未审核通过的文章")
    public ResultVO<PageInfo<PostsDTO>> getBlogNotReviewed(@RequestBody PFUtilDTO pfUtilDTO){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){
            return ResultVO.success(postsService.selectPostsNotReviewed(pfUtilDTO.getPageNum(), pfUtilDTO.getPageNum(),uuid));
        }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/BlogComments")
    @ApiOperation("文章评论")
    public ResultVO<String> blogComments(@RequestParam Integer postsId, @RequestParam Object content,
                                    @RequestParam(defaultValue = "0", required = false) Integer parentContentId){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){
            return ResultVO.success(commentsService.insertComments(uuid, postsId, parentContentId, content));
        }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/BlogUpdateComments")
    @ApiOperation("修改评论")
    public ResultVO<String> blogUpdateComments(@RequestParam Integer postsId, @RequestParam Integer commentsId,
                                               @RequestParam Object content){
        // TODO: 评论表中应该加入点赞
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){
            return ResultVO.success(commentsService.updateComments(uuid, postsId, commentsId, content));
        }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/BlogDeleteComments")
    @ApiOperation("删除评论")
    public ResultVO<String> blogDeleteComments(@RequestParam Integer postsId, @RequestParam Integer commentsId){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(commentsService.deleteComments(uuid, postsId, commentsId)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/BlogGetComments")
    @ApiOperation("获取评论")
    public ResultVO<List<Comments>> blogGetComments(@RequestParam Integer postsId,
                                                    @RequestParam(defaultValue = "0",required = false) Integer sort){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(commentsService.getComments(postsId, sort)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/BlogLikesComments")
    @ApiOperation("点赞评论")
    public ResultVO<String> blogLikesComments(@RequestParam Integer postsId, @RequestParam Integer commentsId){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){
            if (commentsService.select(commentsId)){
                if (commentsService.likesComments(commentsId)){
                    return ResultVO.success(likesContentService.updateLikeNub(uuid, commentsId));
                }
                return ResultVO.error(LIKES_ERROR);
            }
             return ResultVO.error(COMMENT_NOT_EXIST);
        }
        return ResultVO.error(USER_NOT_EXIST);
    }

}

